//
//  PickerViewController.swift
//  foundationDemo
//
//  Created by Zhao Mingzhi on 16/6/27.
//  Copyright © 2016年 HNA. All rights reserved.
//

import Foundation
import UIKit

class PickerViewController:
    UIViewController,
    UIPickerViewDelegate,
    UIPickerViewDataSource
{
    var pickerdata:NSDictionary = ["浙江省":["杭州市","宁波市"],"安徽省":["黄山市","合肥市"]]
    var pickerFirstData:NSArray!
    var pickerSecondData:NSArray!
    @IBOutlet weak var picker: UIPickerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.pickerFirstData = self.pickerdata.allKeys
        
        let selectedData = self.pickerFirstData[0] as! String
        self.pickerSecondData = self.pickerdata[selectedData] as! NSArray
        
        self.picker.dataSource = self
        self.picker.delegate = self
        
    }
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return self.pickerFirstData.count
    }
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if(component == 0 ){
            return self.pickerFirstData.count
        }else{
            return self.pickerSecondData.count
        }
    }
    
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if(component == 0 ){
            return self.pickerFirstData[row] as! String
        }else{
            return self.pickerSecondData[row] as! String
        }
    }
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if(component == 0 ){
            let selectedData =  self.pickerFirstData[row] as! String
            self.pickerSecondData = self.pickerdata[selectedData] as! NSArray
            self.picker.reloadComponent(1)
            
        }

    }
}